require 'gtk2'


class Interface < Gtk::VBox

	type_register

	#dataModified Signal
	signal_new("dataModified", GLib::Signal::RUN_FIRST, nil, nil)
	def signal_do_dataModified 
	end

	def initialize(command)
	
		super()
		self.spacing = 6
		@command = command
		createUI
		show

	end
	
	def createUI
			
		@command.switches.each{|switch|
			pack_start(createSingleSwitch(switch), false)
		}
	
	end
	
	def createSingleSwitch(switch)
	
		box = nil
	
		box = createParameter(switch)
		
		
		if switch.dataFormat.class != Array
			switch.dataFormat = [switch.dataFormat]
		end
		
		switch.dataFormat.each{|data|
		
			if data.class == Value
				box.pack_start(createValue(data), false)
			elsif data.class == List
				box.pack_start(createList(data), false)
			end
			
		}
		
		return box
		
	end
	
	def createParameter(switch)
	
		box = Gtk::HBox.new(false, 6)
		box.show
	
		if not switch.required
			desc = Gtk::CheckButton.new(switch.description || "", false)
		else
			desc = Gtk::Label.new(switch.description)
			desc.xalign = 0
		end
		desc.show
		
		desc.signal_connect("toggled"){
			switch.used = desc.active?
			signal_emit("dataModified")
		}
		
		box.pack_start(desc)
		
		return box
		
	end
	
	def createValue(data)

		spin = Gtk::SpinButton.new(data.min, data.max, 1)
		spin.value = data.value
		
		spin.signal_connect("changed"){
			data.value = spin.value
			signal_emit("dataModified")
		}
		
		spin.show
		return spin
	
	end
	
	def createList(data)
	
		combo = Gtk::ComboBox.new
		data.elements.each{|element|
			combo.append_text(element.text)
		}
		combo.active = data.selected
		
		combo.signal_connect("changed"){
			data.value = data.elements[combo.active].value
			data.selected = combo.active
			signal_emit("dataModified")
		}
		
		combo.show
		return combo
		
	end
	
end
